﻿namespace EFS.RC.CC.Tools.Deployment
{
	/// <summary>
	/// Deployment watch 
	/// </summary>
	class Watch : EFS.Common.Async.RepeaterSlot
	{
		#region Composition

		// sync access
		private object syncAccess = new object();

		// End point to watch
		private EFS.RC.CC.Applications.EndPoints.OfficeService.Operators operatorsOfficeServiceEndPointApplication = null;

		#endregion

		#region Delegates

		/// <summary>
		/// Deployment changes watch
		/// </summary>
		public delegate void WatchDeployment();

		/// <summary>
		/// Deployment watch delegate registrar
		/// </summary>
		public WatchDeployment OnWatchDeployment;

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="operatorsOfficeServiceEndPointApplication"></param>
		public Watch(EFS.RC.CC.Applications.EndPoints.OfficeService.Operators operatorsOfficeServiceEndPointApplication) 
			: base(Config.Settings.DeploymentRefreshPollingMinutes * 60)
		{
			// check
			EFS.Common.Verify.Reference(operatorsOfficeServiceEndPointApplication, "operatorsOfficeServiceEndPointApplication");

			// set internals
			this.operatorsOfficeServiceEndPointApplication = operatorsOfficeServiceEndPointApplication;
		}

		#endregion

		#region Usage

		/// <summary>
		/// Describe this object into a nice formated string
		/// </summary>
		/// <returns></returns>
		public override System.String Describe()
		{
			return System.String.Format("EFS.RC.CC.Tools.Deployment.Watch for {0}", operatorsOfficeServiceEndPointApplication.Describe());
		}

		#endregion

		#region Runtime

		/// <summary>
		/// Unhandled exception
		/// </summary>
		/// <param name="ex"></param>
		protected override void OnException(System.Exception ex)
		{
			// check
			EFS.Common.Verify.Reference(ex, "ex");

			// log
			EFS.EventLog.Log.LogException(ex, this);

			// SAL
			EFS.RC.CC.Tools.Loggers.SAL.Add(null, "OnException", ex, this);
		}

		/// <summary>
		/// Send agents activity status
		/// </summary>
		protected override void RepeateAction()
		{
			// log
			EFS.EventLog.Log.LogInfo("OnWatchDeploymentTimer", this);

			// watch
			if (null != OnWatchDeployment)
			{
				try
				{
					OnWatchDeployment();
				}
				catch (System.Exception ex)
				{
					// log
					EFS.EventLog.Log.LogException(ex, this);
				}
			}
		}

		#endregion // Runtime
	}
}
